class Solution:
    def isSelfCrossing(self, x: list) -> bool:
        if len(x) <= 3:
            return False
        list_line, list_cross = [[0, 0, 0, x[0]]], []
        last_point, direction = [0, x[0]], [-1, 0]
        for num in x[1:]:
            tar_point = [last_point[0] + direction[0] * num, last_point[1] + direction[1] * num]
            if direction[0]:
                for lists in list_line[:-1]:
                    if (lists[1] - last_point[1]) * (lists[3] - last_point[1]) <= 0 and \
                            (last_point[0] - lists[0]) * (tar_point[0] - lists[0]) <= 0:
                        return True
                list_cross.append(last_point + tar_point)
            else:
                for lists in list_cross[:-1]:
                    if (lists[0] - last_point[0]) * (lists[2] - last_point[0]) <= 0 and \
                            (last_point[1] - lists[1]) * (tar_point[1] - lists[1]) <= 0:
                        return True
                list_line.append(last_point + tar_point)
            last_point = tar_point
            if direction == [-1, 0]:
                direction = [0, -1]
            elif direction == [0, -1]:
                direction = [1, 0]
            elif direction == [1, 0]:
                direction = [0, 1]
            else:
                direction = [-1, 0]
        if direction[1]:
            for lists in list_cross[:-1]:
                if lists[1] == tar_point[1] and (tar_point[0] - lists[0]) * (tar_point[0] - lists[2]) <= 0:
                    return True
        else:
            for lists in list_line[:-1]:
                if lists[0] == tar_point[0] and (tar_point[1] - lists[1]) * (tar_point[1] - lists[3]) <= 0:
                    return True
        return False


a = Solution()
print(a.isSelfCrossing([2, 1, 1, 2]))  # T
print(a.isSelfCrossing([1, 2, 3, 4]))  # F
print(a.isSelfCrossing([1, 1, 1, 1]))  # T
print(a.isSelfCrossing([3, 3, 4, 2, 2]))  # F
print(a.isSelfCrossing([1, 1, 2, 1, 1]))  # T
